﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using System.IO;
using System.Xml;

namespace SharePoint.Intranet.AlertImportExport
{
    
    public class AlertDetails
    {
        public AlertDetails()
        {
        }

        public AlertDetails(SPAlert Alert)
        {
            switch (Alert.AlertType)
            {
                case SPAlertType.Item:
                    ItemID = Alert.ItemID;
                    break;
                case SPAlertType.List:
                    ListID = Alert.ListID;
                    break;
                case SPAlertType.Custom:
                    
                    break;
            }
            AlertType = Alert.AlertType.ToString();
            AlertFrequency = Alert.AlertFrequency.ToString();
            if (Alert.AlertFrequency != SPAlertFrequency.Immediate)
            {
                AlertTime = Alert.AlertTime;
            }
            EventType = Alert.EventType.ToString();
            Title = Alert.Title.ToString();
            UserId = Alert.UserId;
        }

        public Guid ListID { get; set; }
        public int ItemID { get; set; }
        public string AlertType { get; set; }
        public string AlertFrequency { get; set; }
        public DateTime AlertTime { get; set; }
        public string EventType { get; set; }
        public string Title { get; set; }
        public int UserId { get; set; }

        public string ToXml()
        {

            StringWriter Output = new StringWriter(new StringBuilder());
            string returnValue = "";

            XmlSerializer s = new XmlSerializer(this.GetType());
            s.Serialize(Output, this);

            returnValue = Output.ToString().Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
            returnValue = returnValue.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
            returnValue = returnValue.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "").Trim();
            
            return returnValue;
        }


    }
}
